package com.tuling.spring;

import com.tuling.UserInterface;
import com.tuling.UserService;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

public class ProxyFactoryBeanDemoB {

	@Bean
	public MethodInterceptor testAroundAdvice(){
		return invocation -> {
			System.out.println("before...");
			Object result = invocation.proceed();
			System.out.println("after...");
			return result;
		};
	}

	@Bean
	public ProxyFactoryBean userService(){
		UserService userService = new UserService();

		ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
		proxyFactoryBean.setTarget(userService);
		proxyFactoryBean.setInterceptorNames("testAroundAdvice");
		return proxyFactoryBean;
	}

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProxyFactoryBeanDemoB.class);
		UserInterface userInterface = (UserInterface) context.getBean("userService");
		userInterface.test();
	}
}
